package com.dpf.hou.controller;


import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dpf.hou.mapper.PetMapper;
import com.dpf.hou.pojo.Pet;
import com.dpf.hou.pojo.User;
import com.dpf.hou.service.PetService;
import com.dpf.hou.util.Base64Util;
import com.dpf.hou.util.ResultUtil;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("pet")
public class PetController {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetService petService;


    @GetMapping("getAllPet")
    public List<Pet> getAllPet(){
        return petService.getAllPet();
    }


    //增
    @PostMapping("addPet")
    public boolean addPet(Pet pet){
        Date d=new Date();
        pet.setDate(d);
        System.out.println(pet);
        return petService.addPet(pet);
    }
    //删
    @DeleteMapping("delPet")
    public boolean delPet(Integer petid){
        return petMapper.deleteById(petid);
    }
    @PutMapping("updatePet")
    public boolean updatePet(Pet pet){
        Date d=new Date();
        pet.setDate(d);
        System.out.println(pet);
        return petService.updatePet(pet);
    }

    @GetMapping("getPage")
    public Page<Pet> getPage(int page,int size){
        Page<Pet> res=petService.getPage(page,size);
        return res;
    }

    @GetMapping("getPageByName")
    public Page<Pet> getPageByName(int page,int size,String petname){
        Page<Pet> res=petService.getPageByName(page,size,petname);
        return res;
    }

    @DeleteMapping("delBatch")
    public boolean delBatch(@RequestParam List<Integer> petid){
        boolean result=petService.delBatch(petid);

        return result;
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception{
        List<Pet> list=petService.list();
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.addHeaderAlias("petid","宠物id");
        writer.addHeaderAlias("petname","宠物名");
        writer.addHeaderAlias("petsex","宠物性别");
        writer.addHeaderAlias("petage","宠物年龄");
        writer.addHeaderAlias("image","图片");
        writer.addHeaderAlias("userid","发布人id");
        writer.addHeaderAlias("date","发布时间");
        writer.addHeaderAlias("status","状态");
        writer.write(list,true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName= URLEncoder.encode("宠物信息","UTF-8");
        response.setHeader("Content-Disposition","attachment;filename="+ fileName +".xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out,true);
        out.close();
        writer.close();
    }

    @PostMapping("/fileImport")
    public boolean fileImport(MultipartFile file) throws Exception{
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<Pet> list = reader.readAll(Pet.class);
        System.out.println(list);
        petService.saveBatch(list);
        return true;
    }

    @PostMapping("upload")
    public ResultUtil upload(MultipartFile uploadFile) {
//声明上传地址
        String path = "d://upload";
//判断上传地址是否存在，如果不存在，要创建
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
//获取上传的文件名
        String fileName = UUID.randomUUID()+uploadFile.getOriginalFilename();
//创建新的文件对象
        File newFile = new File(file, fileName);
//将新的文件对象放到指定位置
        try {
            uploadFile.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ResultUtil resultUtil = null;
//返回给前端当前上传的文件在文件服务器上的地址
        String url = "http://localhost:8888/upload/"+fileName;
//返回给前端当前上传的是图片还是视频
        if (fileName.endsWith("jpg") || fileName.endsWith("png") ||
                fileName.endsWith("gif") || fileName.endsWith("jpeg")) {
//是图片
            resultUtil = new ResultUtil(1,"图片上传成功",url);
        } else {
//是视频
            resultUtil = new ResultUtil(2,"视频上传成功",url);
        }
        return resultUtil;
    }

    @GetMapping("petGet")
    public Page<Pet> petGet(int page,int size,String petname){
        Page<Pet> res=petService.petGet(page,size,petname);
        return res;
    }


    @PutMapping("updateById")
    public boolean updateById(Pet pet){
        return petService.updateById(pet);
    }


//    @PutMapping()




}



